package com.xp.tinyurl.service;

import com.github.pagehelper.PageHelper;
import com.xp.tinyurl.common.cache.ChannelCache;
import com.xp.tinyurl.dao.ChannelDao;
import com.xp.tinyurl.model.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 */
@Service
public class ChannelService {

    @Autowired
    private ChannelDao channelDao;

    @Autowired
    private ChannelCache channelCache;

    public Channel getById(Long channelId) {
        Channel channel = channelCache.getById(channelId);
        if (channel != null) {
            return channel;
        }

        channel = channelDao.selectByPrimaryKey(channelId);
        channelCache.add(channelId, channel);
        return channel;
    }

    public List<Channel> getPageList(Map map) {
        Example example = new Example(Channel.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(map.get("pageSize")) && !StringUtils.isEmpty(map.get("startPage"))){
            PageHelper.startPage((Integer) map.get("startPage"), (Integer) map.get("pageSize"));
        }
        return channelDao.selectByExample(example);
    }

    public Integer getPageListCount(Map map) {
        Example example = new Example(Channel.class);
        Example.Criteria criteria = example.createCriteria();

        return channelDao.selectCountByExample(example);
    }
}
